package com.choudou5.solr.utils;

import com.alibaba.dingtalk.openapi.message.LinkMessage;
import com.choudou5.dingtalk.service.DingTalkAuthService;
import com.choudou5.dingtalk.service.DingTalkAuthServiceImpl;
import com.choudou5.dingtalk.service.DingTalkChatService;
import com.choudou5.dingtalk.service.DingTalkChatServiceImpl;
import com.choudou5.solr.util.AssertUtil;
import com.choudou5.solr.util.ConfigUtil;
import com.choudou5.solr.util.LogDeBugUtil;
import com.choudou5.solr.util.StrUtil;
import com.choudou5.solr.util.notice.INotice;
import com.choudou5.solr.util.thread.ThreadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @name 钉钉工具类
 * @author choudou5
 * @date 2018-07-27
 */
public class DingTalkUtil implements INotice {

    private static final Logger logger = LoggerFactory.getLogger(DingTalkUtil.class);

    //官方 api: https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Hd3OF7&treeId=385&articleId=104980&docType=1

    private static DingTalkAuthService dingTalkAuthService = new DingTalkAuthServiceImpl(ConfigUtil.getStr("dingtalk.corpSecret"), ConfigUtil.getStr("dingtalk.corpId"));
    private static DingTalkChatService dingTalkChatService = new DingTalkChatServiceImpl(dingTalkAuthService);

    private static String ADMIN_CHAT_ID = null;
    private static String OPS_CHAT_ID = SysDictUtil.getOneValueByType("ding_ops_group");

    public static void setAdminGroupId(String adminChatId){
        ADMIN_CHAT_ID = adminChatId;
    }

    public static void setOpsGroupId(String opsChatId){
        OPS_CHAT_ID = opsChatId;
    }


    /**
     * 发送 文本消息
     * @param chatId 钉钉群组ID
     * @param text
     */
    public static void sendTextMsg(String chatId, String text) {
        AssertUtil.isNotBlank(text, "消息不能为空");
        AssertUtil.isNotBlank(chatId, "群组不能为空");
        dingTalkChatService.sendTextMsg(chatId, text);
    }

    /**
     * 发送 文本消息
     * @param text
     */
    public static void sendTextMsgToOps(String text) {
        try {
            sendTextMsgAsync(OPS_CHAT_ID, text);
        } catch (Exception e) {
            logger.error("发送钉钉运维消息失败", e);
        }
    }


    /**
     * 发送 链接消息
     * @param chatId 钉钉群组ID
     * @param linkMessage
     */
    public static void sendLinkMsg(String chatId, LinkMessage linkMessage) {
        dingTalkChatService.sendLinkMsg(chatId, linkMessage);
    }

    public static void sendTextMsgAsync(final String chatId, final String text) {
        AssertUtil.isNotBlank(text, "消息不能为空");
        AssertUtil.isNotBlank(chatId, "群组不能为空");
        ThreadUtil.execAsync(new Runnable() {
            @Override
            public void run() {
                try {
                    dingTalkChatService.sendTextMsg(chatId, text);
                } catch (Exception e) {
                    logger.error("发送钉钉消息失败", e);
                }
            }
        });
    }

    @Override
    public void sendToOpsGroup(String message) {
        logger.info("钉钉通知 运维群 **********");
        if(StrUtil.isBlank(OPS_CHAT_ID)){
            LogDeBugUtil.debug("未配置 运维群ID");
            return;
        }
        sendTextMsg(OPS_CHAT_ID, message);
    }

    @Override
    public void sendToAdminGroup(String message) {
        logger.info("钉钉通知 管理群 **********");
        if(StrUtil.isBlank(ADMIN_CHAT_ID)){
            LogDeBugUtil.debug("未配置 管理群ID");
            return;
        }
        sendTextMsg(ADMIN_CHAT_ID, message);
    }
}
